//缴费通知管理
const express = require("express");
const router = express.Router();
const db = require("../utils/db");

//渲染数据
router.get("/noticepayget", (req, res) => {
    const { search } = req.query;
    const sql = `select * from noticePayment;`;
    const sql2 = `select count(*) as total from noticePayment where projectNames like '%${search}%';`;
    db.query(sql, (err, data) => {
      // console.log(data);
      if (err) {
        res.json({ error: 1, msg: err });
      } else {
        db.query(sql2, (err2, data2) => {
          if (err2) {
            res.json({ error: 1, msg: err2 });
          } else {
            // console.log(data2)
            res.json({ error: 0, data: data, total: data2[0].total });
          }
        });
      }
    });
  });

  //高级筛选
router.post("/noticePayFilter", (req, res) => {
    console.log(req.body);
    const { projectNames, endTime, status_n } = req.body;
    const sql = `SELECT * FROM noticePayment  
      WHERE projectNames LIKE '%${projectNames}%' AND endTime <= '${endTime}' AND status_n LIKE '%${status_n}%';`;
    db.query(sql, (err, data) => {
      console.log(data);
      if (err) {
        res.send({ error: 1, msg: err });
      } else {
        res.send({ error: 0, data: data });
      }
    });
  });

  //删除
  router.post("/noticePaydel", (req, res) => {
    console.log(req.body);
    const { key } = req.body;
    const sql = `delete from noticePayment where key_n=${key};`;
    db.query(sql, (err, data) => {
      console.log(data);
      if (err) {
        res.send({ error: 1, msg: err });
      } else {
        res.send({ error: 0, data: data });
      }
    });
  });

  //发起通知
  router.post("/noticePayEdit", (req, res) => {
    console.log(req.body);
    const { key_n ,status_n} = req.body;
    const sql = `update noticePayment set status_n='${status_n}' where key_n='${key_n}'`;
    db.query(sql, (err, data) => {
      console.log(data);
      if (err) {
        res.send({ error: 1, msg: err });
      } else {
        res.send({ error: 0, data: data });
      }
    });
  });

  //更改时间后，发起通知
  router.post("/noticePayEditTime", (req, res) => {
    console.log(req.body);
    const { key_n ,endTime,status_n} = req.body;
    const sql = `update noticePayment set status_n='${status_n}', endTime ='${endTime}'where key_n='${key_n}'`;
    db.query(sql, (err, data) => {
      console.log(data);
      if (err) {
        res.send({ error: 1, msg: err });
      } else {
        res.send({ error: 0, data: data });
      }
    });
  });



// 新建
router.post("/noticePayAdd", (req, res) => {
  const { projectNames, noticeContent, endTime,notifyInitiator,status_n } = req.body;
  const sql = `insert into noticePayment value(null,'${projectNames}','${noticeContent}','${endTime}','${notifyInitiator}','${status_n}');`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ error: 1, msg: err });
    } else {
      res.send({ error: 0, msg: "添加成功" });
    }
  });
});

module.exports = router;